/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package system.database.mysql5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.aionemu.commons.database.DatabaseFactory;
import com.aionemu.gameserver.dao.PlayerEmotionListDAO;
import com.aionemu.gameserver.dao.PlayerTitleListDAO;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.TitleList;
import com.aionemu.gameserver.model.gameobjects.player.TitleListEntry;

/**
 * @author xavier
 * 
 */
public class MySQL5PlayerTitleListDAO extends PlayerTitleListDAO
{
	/** Logger */
	private static final Logger	log				= Logger.getLogger(PlayerEmotionListDAO.class);

	private static final String	SELECT_QUERY		= "SELECT `title_id`, `remaining` FROM `player_titles` WHERE `player_id`=?";
	private static final String	INSERT_QUERY	= "INSERT INTO `player_titles`(`player_id`,`title_id`, `remaining`) VALUES (?,?,?)";

	@Override
	public TitleList loadTitleList(final int playerId)
	{

		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(SELECT_QUERY);
			stmt.setInt(1, playerId);
			ResultSet rset = stmt.executeQuery();
			TitleList titleList = new TitleList();
			while (rset.next())
			{
				int titleId = rset.getInt("title_id");
				int remaining = rset.getInt("remaining");
				titleList.addEntry(titleId, remaining);
			}
			rset.close();
			stmt.close();
			return titleList;
		}
		catch (Exception e)
		{
			log.fatal("Could not restore emotionId for playerObjId: " + playerId + " from DB: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
		return null;
	}

	@Override
	public boolean storeTitle(Player player, TitleListEntry entry)
	{
		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(INSERT_QUERY);
			stmt.setInt(1, player.getObjectId());
			stmt.setInt(2, entry.getId());
			stmt.setInt(3, entry.getDispearTime());
			stmt.execute();
			stmt.close();
		}
		catch (Exception e)
		{
			log.fatal("Could not store emotionId for player " + player.getObjectId() + " from DB: " + e.getMessage(), e);
			return false;
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	return true;
	}

	@Override
	public boolean supports(String databaseName, int majorVersion, int minorVersion)
	{
		return MySQL5DAOUtils.supports(databaseName, majorVersion, minorVersion);
	}

}
